﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ARCam3D.Imaging.Processing
{
    public class ImageBinarizator
    {
        private byte threshold = 60;

        unsafe public BinaryImage Binarizate(RGBImage image)
        {
            BinaryImage result = new BinaryImage(image.Width, image.Height);

            int bytesPerPixel = RGBImage.BytesPerPixel;
            int redOffset = RGBImage.RedOffset;
            int greenOffset = RGBImage.GreenOffset;
            int blueOffset = RGBImage.BlueOffset;

            long pointsCount = image.Width * image.Height;
            byte* imageDataPointer = (byte*)image.DataPtr.ToPointer();
            byte* resultDataPointer = (byte*)result.DataPtr.ToPointer();
            for (long point = 0; point < pointsCount;
                ++point, imageDataPointer += bytesPerPixel, ++resultDataPointer)
            {
                byte value =  (byte)((imageDataPointer[redOffset] + imageDataPointer[greenOffset] + imageDataPointer[blueOffset]) / 3);
                *resultDataPointer = (byte)(value > threshold ? 0xFF : 0);
            }

            return result;
        }
    }
}
